<template>
  <div class="app-container-item">
    <div class="form-main">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="form-flex">
        <el-row :gutter="20">
          <el-col :span="8" :xs="24">
            <el-form-item label="所属小区" prop="communityName">
              <el-input v-model="form.communityName" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="所属楼栋" prop="floorName">
              <el-input v-model="form.floorName" disabled/>
            </el-form-item>
          </el-col>
          <!--<el-col :span="8" :xs="24">-->
            <!--<el-form-item label="单元编号" prop="unitNum">-->
              <!--<el-input v-model="form.unitNum" placeholder="请输入单元编号"/>-->
            <!--</el-form-item>-->
          <!--</el-col>-->
          <el-col :span="8" :xs="24">
            <el-form-item label="单元名称" prop="unitName">
              <el-input v-model="form.unitName" placeholder="请输入单元名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="总层数" prop="layerNum">
              <el-input v-model="form.layerNum" placeholder="请输入总层数"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="单元面积" prop="unitArea">
              <el-input v-model="form.unitArea" placeholder="请输入单元面积"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="房间数量" prop="roomCountNum">
              <el-input v-model="form.roomCountNum"  disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="入住数量" prop="roomYrzNum">
              <el-input v-model="form.roomYrzNum" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24">
            <el-form-item label="未入住数量" prop="roomWrzNum">
              <el-input v-model="form.roomWrzNum" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="24" :xs="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="dialog-footer" style="text-align: center;">
      <el-button type="primary" plain @click="submitForm" v-hasPermi="['property:unit:edit']">保 存</el-button>
    </div>
  </div>
</template>

<script>
  import {getUnit, delUnit , updateUnit } from "@/api/property/unit";
  import { censusRoomInfo } from "@/api/property/room";

  export default {
    name: "UnitFOrm",
    data() {
      return {
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          communityId: null,
          deptId: null,
          floorId: null,
          unitNum: null,
          unitName: null,
          layerNum: null,
          unitArea: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          communityId: [
            {required: true, message: "小区ID不能为空", trigger: "blur"}
          ],
          floorId: [
            {required: true, message: "楼栋ID不能为空", trigger: "blur"}
          ],
        }
      };
    },
    watch: {
      currentNode(v) {
        this.initForm()
      }
    },
    created() {
      this.initForm()
    },
    methods: {
      initForm() {
        this.reset();
        if (this.currentNode.modelType == 'unit') {
          getUnit(this.currentNode.id).then(response => {
            this.form = Object.assign(response.data,{roomCountNum:0,roomWrzNum:0,roomYrzNum:0})
            censusRoomInfo({unitId: this.currentNode.id}).then(res=>{
              this.form.roomCountNum = res.data.roomCountNum
              this.form.roomWrzNum = res.data.roomWrzNum
              this.form.roomYrzNum = res.data.roomYrzNum
            })
          });
        }
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          communityId: null,
          deptId: null,
          floorId: null,
          unitNum: null,
          unitName: null,
          layerNum: null,
          unitArea: null,
          dataSourceId: null,
          ancestors: null,
          statusCd: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateUnit(this.form).then(response => {
              this.msgSuccess("修改单元资料成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除单元信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delUnit(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
    },
    props: {
      currentNode: {
        type: Object,
        default:()=>{}
      },
    }
  };
</script>
